package cn.wx.read.utils;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.ogm.model.Result;
import cn.wx.common.utils.JSONUtils;

public class N2OUtils {
	
	/**
	 * 将result 转成 list
	 * @param rt
	 * @param m
	 * @return
	 */
	public static <M>List<M> covetResultToDTO(Result rt,Class<M> c){
		if(rt == null) {
			return new ArrayList<>();
		}
		String jsonStr = JSONUtils.c(rt);
		return (List<M>) JSONUtils.pa(jsonStr, c);
	}
	
	
	/**
	 * 将一组数据平均分成n组
	 *
	 * @param source 要分组的数据源
	 * @param n      平均分成n组
	 * @param <T>
	 * @return
	 */
	public static <T> List<List<T>> averageAssign(List<T> source, int n) {
	    List<List<T>> result = new ArrayList<List<T>>();
	    int remainder = source.size() % n;  //(先计算出余数)
	    int number = source.size() / n;  //然后是商
	    int offset = 0;//偏移量
	    for (int i = 0; i < n; i++) {
	        List<T> value = null;
	        if (remainder > 0) {
	            value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
	            remainder--;
	            offset++;
	        } else {
	            value = source.subList(i * number + offset, (i + 1) * number + offset);
	        }
	        result.add(value);
	    }
	    return result;
	}
	
}
